package com.ktgj.flight.controller;

import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.web.page.TableDataInfo;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;

import com.ktgj.flight.domain.FlightAdjust;
import com.ktgj.flight.domain.FlightAdjustPassenger;
import com.ktgj.flight.domain.vo.AdjustPassengerVo;
import com.ktgj.flight.domain.vo.FlightAdjustPassengerListVo;
import com.ktgj.flight.domain.vo.FlightAdjustPassengerVo;
import com.ktgj.flight.domain.vo.FlightAdjustVo;
import com.ktgj.flight.service.IFlightAdjustPassengerService;
import com.ktgj.flight.service.IFlightAdjustService;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 出票渠道调整Controller
 * 
 * @author ruoyi
 * @date 2021-11-10
 */
@ControllerAdvice
@RestController
@RequestMapping("/adjust")
public class FlightAdjustController extends BaseController
{
    @Autowired
    private IFlightAdjustService flightAdjustService;

    @Autowired
    private IFlightAdjustPassengerService flightAdjustPassengerService;

    /**
     * 查询出票渠道调整列表
     */
    //@PreAuthorize(hasPermi = "flight:adjust:list")
    @GetMapping("/list")
    public TableDataInfo list(FlightAdjust flightAdjust)
    {
        startPage();
        List<FlightAdjust> list = flightAdjustService.selectFlightAdjustList(flightAdjust);
        return getDataTable(list);
    }

    /**
     * 导出出票渠道调整列表
     */
    //@PreAuthorize(hasPermi = "flight:adjust:export")
    @Log(title = "出票渠道调整", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FlightAdjust flightAdjust) throws IOException
    {
        List<FlightAdjust> list = flightAdjustService.selectFlightAdjustList(flightAdjust);
        ExcelUtil<FlightAdjust> util = new ExcelUtil<FlightAdjust>(FlightAdjust.class);
        util.exportExcel(response, list, "出票渠道调整数据");
    }

    /**
     * 获取出票渠道调整详细信息
     */
    //@PreAuthorize(hasPermi = "flight:adjust:query")
    @GetMapping(value = "/{adjustId}")
    public AjaxResult getInfo(@PathVariable("adjustId") Integer adjustId)
    {
        return AjaxResult.success(flightAdjustService.selectFlightAdjustById(adjustId));
    }

    /**
     * 新增出票渠道调整
     */
    //@PreAuthorize(hasPermi = "flight:adjust:add")
    @Log(title = "出票渠道调整", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FlightAdjust flightAdjust)
    {
        return toAjax(flightAdjustService.insertFlightAdjust(flightAdjust));
    }

    /**
     * 修改出票渠道调整
     */
    //@PreAuthorize(hasPermi = "flight:adjust:edit")
    @Log(title = "出票渠道调整", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FlightAdjust flightAdjust)
    {
        return toAjax(flightAdjustService.updateFlightAdjust(flightAdjust));
    }

    /**
     * 删除出票渠道调整
     */
    //@PreAuthorize(hasPermi = "flight:adjust:remove")
    @Log(title = "出票渠道调整", businessType = BusinessType.DELETE)
	@DeleteMapping("/{adjustIds}")
    public AjaxResult remove(@PathVariable Integer[] adjustIds) {
        return toAjax(flightAdjustService.deleteFlightAdjustByIds(adjustIds));
    }

    /**
     * 渠道调整列表
     * */
    @PostMapping("/flightAdjustList")
    public TableDataInfo flightAdjustList(@RequestBody FlightAdjustVo flightAdjustVo) {
        startPage();
        List<FlightAdjust> flightAdjusts = flightAdjustService.flightAdjustList(flightAdjustVo);
        return getDataTable(flightAdjusts);
    }

    /**
     * 新增渠道调整
     * */
    @PostMapping("/addFlightAdjust")
    public AjaxResult addFlightAdjust(HttpServletRequest request, @RequestBody FlightAdjust flightAdjust) {
        int i;
        try {
            i = flightAdjustService.addFlightAdjust(request, flightAdjust);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
        if (i > 0){
            return AjaxResult.success("添加成功");
        }
        else {
            return AjaxResult.error("添加失败");
        }
    }

    /**
     * 上传文件到服务器并反填
     * */
    @PostMapping("/flightAdjustPassengerVo")
    public AjaxResult flightAdjustPassengerVo(MultipartFile file) {
        List<FlightAdjustPassengerVo> flightAdjustPassengerVoList;
        try {
            flightAdjustPassengerVoList = flightAdjustService.flightAdjustPassengerVo(file);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

        return AjaxResult.success(flightAdjustPassengerVoList);
    }

    /**
     * 查询待修改的渠道调整信息
     * */
    @PostMapping("/selectAdjustPassengerVo")
    public AjaxResult selectAdjustPassengerVo(@RequestBody Map<String, Object> map){
         Long id = Long.valueOf(map.get("id").toString());
        AdjustPassengerVo adjustPassengerVo = flightAdjustService.selectAdjustPassengerVo(id);
        return AjaxResult.success(adjustPassengerVo);
    }

    /**
     * 查询待修改的渠道调整大客户列表信息
     * */
    @PostMapping("/adjustPassengerVo")
    public TableDataInfo adjustPassengerVo(@RequestBody Map<String, Object> map){
        startPage();
        Long id = Long.valueOf(map.get("id").toString());
        List<FlightAdjustPassenger> flightAdjustPassengers = flightAdjustPassengerService.flightAdjustPassengerList(id);
        return getDataTable(flightAdjustPassengers);
    }

    /**
     * 修改渠道调整信息
     * */
    @PostMapping("/updateAdjustPassengerVo")
    public AjaxResult updateAdjustPassengerVo(HttpServletRequest request, @RequestBody FlightAdjust flightAdjust){
        int i;
        try {
            i = flightAdjustService.updateAdjustPassengerVo(request, flightAdjust);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

        if (i > 0){
            return AjaxResult.success("修改成功");
        }
        else {
            return AjaxResult.error("修改失败");
        }
    }

    /**
     * 修改渠道调整大客户列表信息
     * */
    @PostMapping("/updatePassengerVo")
    public AjaxResult updatePassengerVo(HttpServletRequest request, @RequestBody FlightAdjustPassengerListVo flightAdjustPassengerList){
        int i;
        try {
            i = flightAdjustPassengerService.updatePassengerVo(request, flightAdjustPassengerList);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

        if (i > 0){
            return AjaxResult.success("修改成功");
        }
        else {
            return AjaxResult.error("修改失败");
        }
    }

    /**
     * 删除渠道调整信息及附属大客户信息
     * */
    @PostMapping("/delAdjustPassengerVo")
    public AjaxResult delAdjustPassengerVo(@RequestBody Map<String, Object> map){
        Long id = Long.valueOf(map.get("id").toString());
        int i;
        try {
            i = flightAdjustService.delAdjustPassengerVo(id);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

        if (i > 0){
            return AjaxResult.success("删除成功");
        }
        else {
            return AjaxResult.error("删除失败");
        }
    }
}
